const app = getApp()
const constant = require("../../../../constant.js")
const storage = require("../../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  bindFormSubmit: function (e) {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.couponId = options.couponId;
    this.setData({
      host: constant.api(app.globalData.pord).url,
      unionid: wx.getStorageSync(storage.keys.unionid),
      effectiveStart: options.effectiveStart,
      effectiveEnd: options.effectiveEnd
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  formSubmit: function (e) {
    var that = this;
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var data = e.detail.value;

    if (e.detail.value.name.trim() == '') {
      wx.showModal({
        content: "请填写姓名",
        showCancel: false,
      })
      return false;
    } else if (e.detail.value.mobile.trim() == '') {
      wx.showModal({
        content: "请填写手机号",
        showCancel: false,
      })
      return false;
    } else {
      wx.request({
        url: that.data.host + '/profile/coupon/' + that.data.couponId + '/give?unionid=' + that.data.unionid,
        method: 'POST',
        data: data,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.code == "0") {
            wx.showToast({
              title: res.data.message,
              icon: 'success',
              duration: 1000,
              mask: true,
              success: function () {
                wx.navigateBack({
                  delta: 1
                });
              }
            });
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 1000,
              mask: true
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '转赠失败',
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      });
    }
  }
})